from django.http import HttpResponse
from django.shortcuts import render
from settings import RESOURCE_FOLDER
import json
from spot_data_service.spot_data_manager import spot_data_manager
from spot_data_service.spot_AM_Report_analyzer import attainment_statistics

am_report_config_path = RESOURCE_FOLDER + 'AM_Report.config.json'
tsl_config_path = RESOURCE_FOLDER + 'TSLnew.config.json'
order_funnal_path = RESOURCE_FOLDER + 'order funnel analysis table.config.json'
prod_data_path = RESOURCE_FOLDER + 'ProdData.config.json'
area_data_path = RESOURCE_FOLDER + 'area.config.json'
asl_data_path = RESOURCE_FOLDER + 'ASL new.config.json'
plan_data_path = RESOURCE_FOLDER + 'Plan new.config.json'

filenames = [ 
    am_report_config_path, 
    tsl_config_path, 
    order_funnal_path, 
    prod_data_path,
    asl_data_path,
    area_data_path,
    plan_data_path ]
spot_data_manager.get_instance().initialize(filenames)

# Create your views here.
def index(request):
    return HttpResponse("You're at the dashboard data service index.")

def AM_report(request):
    res = {}
    res['status'] = False
    res['exception'] = 'Deprecated'
    tmp = 'AM_report(' + json.dumps(res) + ');'
    return HttpResponse(tmp, content_type="application/json")

def tsl(request):
    year = request.GET['year']
    quarter = request.GET['quarter']
    theater = request.GET['theater']
    if 'title' in request.GET:
        title = request.GET['title']
    else:
        title = ''
    if 'top' in request.GET:
        top_cnt = int(request.GET['top'])
    else:
        top_cnt = 0
    res = spot_data_manager.get_instance().attainment_details(year, quarter, theater, title, top_cnt)
    tmp = 'tsl(' + json.dumps(res) + ');'
    return HttpResponse(tmp, content_type='application/json')

def AMPerformance(request):
    year = request.GET['year']
    quarter = request.GET['quarter']
    theater = request.GET['theater']
    index = int(request.GET['index'])
    if 'title' in request.GET:
        title = request.GET['title']
    else:
        title = ''
    if 'top' in request.GET:
        top_cnt = int(request.GET['top'])
    else:
        top_cnt = 0
    res = spot_data_manager.get_instance().am_performance(year, quarter, theater, title, top_cnt, index)
    tmp = 'AMPerformance(' + json.dumps(res) + ');'
    return HttpResponse(tmp, content_type='application/json')

def order_funnal(request):
    res = spot_data_manager.get_instance().order_funnal_details()
    tmp = 'order_funnal(' + json.dumps(res) + ');'
    return HttpResponse(tmp, content_type='application/json')

def prod_data(request):
    year = request.GET['year']
    quarter = request.GET['quarter']

#    try:
    res = {}
    if 'search_key' in request.GET:
        index_key = request.GET['search_key']
        if 'value' in request.GET:
            value_key = request.GET['value']
        else:
            value_key = ''
        res = spot_data_manager.get_instance().prod_data_details(str(year), str(quarter), index_key, value_key)
        # print res
    else:
        res = spot_data_manager.get_instance().prod_data_details(str(year), str(quarter))
    res['status'] = True
    tmp = 'prod_data(' + json.dumps(res) + ');'
    # except Exception, ex:
    #     print ex
    #     res = {}
    #     res['status'] = False
    #     res['Exception'] = str(ex)
    #     tmp = 'prod_data(' + json.dumps(res) + ');'
    return HttpResponse(tmp, content_type='application/json')

def theater_bcpr(request):
    theater = request.GET['theater']
    year = request.GET['year']
    quarter = request.GET['quarter']
    res = spot_data_manager.get_instance().bcpr_details(theater, year, quarter)
    tmp = 'theater_bcpr(' + json.dumps(res) + ');'
    return HttpResponse(tmp, content_type='application/json')